package com.yazi.mxz.common.db.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import org.hibernate.Criteria;
import org.springframework.stereotype.Repository;

import com.yazi.mxz.common.db.dao.GoodsReportViewDao;
import com.yazi.mxz.common.entity.custom.GoodsReportView;
import com.myframework.smhj.hibernate.Page;
import com.myframework.smhj.hibernate.Searching;
import com.myframework.smhj.hibernate.dao.BasicDao;

@Repository("goodsReportViewDao")
public class GoodsReportViewDaoImpl extends BasicDao<GoodsReportView> implements GoodsReportViewDao {

	
	public GoodsReportViewDaoImpl() {
		super(GoodsReportView.class);
	}

	@Override
	public Criteria getExample(GoodsReportView revenue, String searchWord) {
		return null;
	}

	@Override
	public List<GoodsReportView> listRevenueByCompanyId4Goods(long companyId, long startTime,
			long endTime, Searching si) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("company_id", companyId);
		param.put("start_time", startTime);
		param.put("end_time", endTime);
		return listWithCustomCache("listRevenueByCompanyId4GoodsSql", param, si);
	}

	@Override
	public List<GoodsReportView> listLossByCompanyId4Goods(long companyId, long startTime,
			long endTime, Searching si) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("company_id", companyId);
		param.put("start_time", startTime);
		param.put("end_time", endTime);
		return listWithCustomCache("listLossByCompanyId4GoodsSql", param, si);
	}
	
	@Override
	public List<GoodsReportView> listByCompanyId4Store(long companyId, long startTime, long endTime, Searching si) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("company_id", companyId);
		param.put("start_time", startTime);
		param.put("end_time", endTime);
		return listWithCustomCache("listByCompanyId4StoreSql", param, si);
	}

	@Override
	public List<GoodsReportView> sumByCompanyId(long companyId, long startTime, long endTime) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("company_id", companyId);
		param.put("start_time", startTime);
		param.put("end_time", endTime);
		return listWithCustomCache("sumByCompanyIdSql", param, null);
	}

	@Override
	public List<GoodsReportView> listRevenueByCompanyId4Day(long companyId, long startTime,
			long endTime, Searching si) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("company_id", companyId);
		param.put("start_time", startTime);
		param.put("end_time", endTime);
		return listWithCustomCache("listRevenueByCompanyId4DaySql", param, si);
	}

	@Override
	public List<GoodsReportView> listRevenueByStoreId4Goods(long storeId, long startTime,
			long endTime, Searching si) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("store_id", storeId);
		param.put("start_time", startTime);
		param.put("end_time", endTime);
		return listWithCustomCache("listRevenueByStoreId4GoodsSql", param, si);
	}

	@Override
	public List<GoodsReportView> listLossByStoreId4Goods(long storeId, long startTime,
			long endTime, Searching si) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("store_id", storeId);
		param.put("start_time", startTime);
		param.put("end_time", endTime);
		return listWithCustomCache("listLossByStoreId4GoodsSql", param, si);
	}

	@Override
	public List<GoodsReportView> listByStoreId4Checker(long storeId, long startTime, long endTime,
			Searching si) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("store_id", storeId);
		param.put("start_time", startTime);
		param.put("end_time", endTime);
		return listWithCustomCache("listByStoreId4CheckerSql", param, si);
	}

	@Override
	public List<GoodsReportView> sumByStoreId(long storeId, long startTime, long endTime) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("store_id", storeId);
		param.put("start_time", startTime);
		param.put("end_time", endTime);
		return listWithCustomCache("sumByStoreIdSql", param, null);
	}

	@Override
	public List<GoodsReportView> listRevenueByStoreId4Day(long storeId, long startTime, long endTime,
			Searching si) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("store_id", storeId);
		param.put("start_time", startTime);
		param.put("end_time", endTime);
		return listWithCustomCache("listRevenueByStoreId4DaySql", param, si);
	}
	
	@SuppressWarnings("unchecked")
	private List<GoodsReportView> listWithCustomCache(String queryName, Map<String, Object> params, Searching si) {
		

		final String cacheName = "GoodsReportView4Chart";
		
		StringBuffer keySb = new StringBuffer(queryName);
		for(Map.Entry<String, Object> entry : params.entrySet()) {
			keySb.append("_" + entry.getValue());
		}
		final String elkey = keySb.toString();
		
		List<GoodsReportView> list = new ArrayList<GoodsReportView>();
		CacheManager cm = CacheManager.getInstance();
		Cache cache;
		if (cm.cacheExists(cacheName)) {
			cache = cm.getCache(cacheName);
			Element element = cache.get(elkey);
			if (null != element && !element.isExpired()) {
				list = (List<GoodsReportView>)element.getValue();
			}
		} else {
			cm.addCache(cacheName);
			cache = cm.getCache(cacheName);
		}
		if (list.size() == 0) {
			Page<GoodsReportView> page = list(queryName, params, si);
			cache.put(new Element(elkey, page.getList()));
		}
		
		return list;
	}

}
